package cc.verywell.pureblock.msg.nfs.linkmap;

import java.io.Closeable;
import java.io.IOException;
import java.util.concurrent.ConcurrentSkipListMap;

import org.iq80.leveldb.impl.InternalKey;
import org.iq80.leveldb.util.Slice;

import cc.verywell.pureblock.msg.nfs.map.HDmap;
import cc.verywell.pureblock.msg.nfs.map.HDmap.HDPUT;

/**
* 
* Copyright © 2019 TanYaqiu. All rights reserved.
* @author TanYaqiu 
* @address flat 601
* @date 2019年6月16日 上午4:47:51
*/
public class LinkMapHd implements LinkMap,Closeable{

	private final HDmap hdmap;
	private final EasyIndex db;

	public LinkMapHd(HDmap hdmap, EasyIndex db) {
		this.hdmap = hdmap;
		this.db = db;
	}
	
	@Override
	public void close(){
		//没有?this.hdmap.close();
		this.db.close();
	}
	
	final private Object linkSync = new Object();
	
	@Override
	public PUT put(byte[] key, byte[] value, boolean isStable) {
		boolean isEXIST = false;
		switch(hdmap.hdput(key, value)) {
		case EXIST:
			isEXIST = true;
		case SUCCESS:
		case OTHER_SUCCESS:
			break;
		case OTHER_FAILD:
		case ERROR:
			return PUT.FAILD;
		case NOSPACE:
			return PUT.NOSPACE;
		}
		if(isStable || value[0]!=0) {
			//LinkCell link0 = LinkCell.createLink0(key);
			synchronized (linkSync) {
				if(isStable) {
					db.put(key);
					
					LinkCell stableCell = new LinkCell(key, seq++, false);
					memsize += stableCell.encodeSize();
					memmap.put(stableCell, null);
					//TODO 写入自身stable记录
					if(value[0]!=0) {
						
						
						//TODO 写入link记录
					}
				}
				if(value[0]!=0) {
					EasyIterator it = db.get(LinkCell.createKey(key));
					while(it.)
					
					//TODO 检查是否为某个stable的link0文件;
				}
			}
		}
		return isEXIST?PUT.EXIST:PUT.SUCCESS;
	}
	
	public Iterator<LinkCell> getIterator
	
	

}
